var myMap, params;
require([
        "dojo/dom", "dojo/_base/array", "esri/Color", "dojo/parser", "dijit/registry", "esri/map", "esri/lang",
        "esri/graphic", "esri/InfoTemplate", "esri/layers/GraphicsLayer", "esri/renderers/SimpleRenderer", "esri/geometry/Point",
        "esri/tasks/FeatureSet", "esri/tasks/RouteTask", "esri/tasks/RouteParameters", "esri/symbols/SimpleMarkerSymbol",
        "esri/symbols/SimpleLineSymbol", "dijit/form/ComboBox", "dijit/layout/BorderContainer", "dijit/layout/ContentPane"],
    function (dom, array, Color, parser, registry, Map, esriLang, Graphic, InfoTemplate, GraphicsLayer, SimpleRenderer, Point,
              FeatureSet, RouteTask, RouteParameters, SimpleMarkerSymbol, SimpleLineSymbol) {

        var routeTask, stops, routeGraphicLayer, lastRoute;
        //var  = new FeatureSet([]);
        //var barriers = new FeatureSet([]);
        var stopsGraphicsLayer = new GraphicsLayer(); //stop点图层
        // var lastRoute = new Graphic();
        //inputsLayer.clear();
        var features = [];

        parser.parse();

        esri.config.defaults.io.corsDetection = false; //跨域问题
        //esri.config.defaults.io._processedCorsServers[“你的server url”]=1;

        myMap = new esri.Map("mapDiv");
        var baseMap = new esri.layers.ArcGISDynamicMapServiceLayer("http://dongyue/ArcGIS/rest/services/NAService/MapServer");
        myMap.addLayer(baseMap);

        routeTask = new esri.tasks.RouteTask("http://dongyue/ArcGIS/rest/services/NAService/NAServer/Route");

        myMap.on("click", mapClickHandler);

        myMap.on("load", function (evtObj) {
            var myMap = evtObj.target;
            var stopSymbol = new esri.symbol.SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 16,
                new esri.symbol.SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new esri.Color([89, 95, 35]), 2),
                new esri.Color([130, 159, 83, 0.40]));
            stopsGraphicsLayer = new GraphicsLayer();

            var stopRenderer = new esri.renderer.SimpleRenderer(stopSymbol);
            stopsGraphicsLayer.setRenderer(stopRenderer);
            myMap.addLayer(stopsGraphicsLayer);

            routeGraphicLayer = new GraphicsLayer();
            var routeSymbol = new esri.symbol.SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new esri.Color([89, 95, 35]), 4.0);
            var routeRenderer = new esri.renderer.SimpleRenderer(routeSymbol);
            routeGraphicLayer.setRenderer(routeRenderer);
            myMap.addLayer(routeGraphicLayer);
            // params.outSpatialReference = myMap.spatialReference;
        });

        function mapClickHandler(evt) {
            var stopP = new esri.geometry.Point(evt.mapPoint.x, evt.mapPoint.y, myMap.spatialReference);
            var stopPoint = new esri.Graphic(stopP);
            //stopsGraphicsLayer = new esri.layers.GraphicsLayer();
            stopsGraphicsLayer.add(stopPoint);
            features.push(stopPoint);

            if (features.length > 1) {
                stops = new FeatureSet();
                stops.features = features;
                params = new esri.tasks.RouteParameters();
                params.stops = stops;
                params.returnStops = true;
                params.returnRoutes = true;
                params.returnDirections = true;
                params.outSpatialReference = myMap.spatialReference;

                myMap.graphics.enableMouseEvents();

                routeGraphicLayer.on("mouse-over", function (evt) {
                    //clear existing directions and highlight symbol
                    myMap.graphics.clear();
                    dom.byId("directionsDiv").innerHTML = "Hover over the route to view directions";

                    var highlightSymbol = new esri.symbol.SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([0, 255, 255], 0.25), 4.5);
                    var highlightGraphic = new esri.Graphic(evt.graphic.geometry, highlightSymbol);

                    myMap.graphics.add(highlightGraphic);
                    dom.byId("directionsDiv").innerHTML = esriLang.substitute(evt.graphic.attributes, "${*}");
                });

                //solve
                routeTask.solve(params, function (solveResult) {
                    var routeResult = solveResult.routeResults[0];
                    lastRoute = new esri.Graphic();
                    lastRoute = routeResult.route;
                    routeGraphicLayer.add(lastRoute);
                    //lastRoute.toolTip = routeResult.routeName;
                    if (routeResult.route.attributes.Total_Time) {
                        lastRoute.toolTip += " in " + Math.round(Number(routeResult.route.attributes.Total_Time)) + " minutes.";
                    }
                    var directions = solveResult.directions;

                    array.forEach(solveResult.routes, function (route, index) {
                        //build an array of route info
                        var attr = array.map(solveResult.directions[index].features, function (feature) {
                            return feature.attributes.text;
                        });
                        var infoTemplate = new InfoTemplate("Attributes", "${*}");
                        route.setInfoTemplate(infoTemplate);
                        route.setAttributes(attr);
                        routeGraphicLayer.add(route);
                        dom.byId("directionsDiv").innerHTML = "Hover over the route to view directions";

                    });
                });
            }
        }
    }); 